
// 退出登录
let exit =document.querySelector('.exit')
let login = document.querySelector('#login_a')
console.log(exit);
if(localStorage.getItem('username') ){
    login.innerHTML ="您已登录"
    exit.style.display='inline-block'
}
console.log(exit);
exit.onclick = function(){
    console.log(1);
    localStorage.removeItem('username')
  localStorage.removeItem('pass')
  login.innerHTML="登录" 
  exit.style.displa ="none"
}
login.onclick =function(){
    if (!localStorage.getItem('username')) {
        location.href="http://localhost:2205/html/login.html"
    }
}
//免登陆1
var logintimes=window.localStorage.getItem("logintimes")
var Nowtimes = Date.parse(new Date());
window.localStorage.setItem("logintimes",logintimes)
//86400000是一天
//604800000是一周
if(Number(logintimes)+604800000<=Nowtimes){
localStorage.removeItem('username')
localStorage.removeItem('pass')
login.innerHTML="登录" 
exit.style.displa ="none"
}
 // 购物车渲染部分
class more {
    constructor() {
        this.paging_list = document.querySelector('.paging_list')
        console.log( this.paging_list);
        this.container = document.querySelector('.container')
        this.cartNum = document.querySelector('.cart_num')
        this.init()
    }
    init() {
        this.request()
        this.totalNum()
    }
    // 利用ajax向后台发送数据渲染页面
    request() {
        pAjax({url: 'http://localhost:2205/libs/goods.json'})
        .then(data=>{
            let res = JSON.parse(data).slice(2, 26)
            this.render(res)
        })
        
    }
    render(data) {
         data.forEach(item => {
            this.paging_list.innerHTML += `
     <li>
            <a href="http://localhost:2205/html/details.html?id=${item.goods_id}" style="text-decoration:none; color:black;">
            <div class="pic">
                <img src="${item.img_small_logo}">
            </div>
            <p class="title hidea">${item.title}</p>
            </a>
            <p class="price">${item.price}</p>
            <button data-id=${item.goods_id}>加入购物车</button>
        </li>
     `
        }); 
        $(".paging_list").paging({
            PageNum: 8, //每页显示数目
            pageMax: true, //按钮长度是否显示
            pageMaxHideShow: false, //在最后一个的时候是否隐藏按钮长度
            pageDownUpHide: false, //到第一个或最后一个是否让上一页或下一页消失
            pageInput: true, //是否使用文本框输入跳转
            pagingBtnHide: false, //是否让按钮变为一个
            pagingBtnPaging: true,//按钮是否分页
            pagingDisplay: "block"//显示的属性，弹性盒子还是块化
        }, 'json')
        this.addEvent(data)
        this.totalNum()
    }
    // 点击加入购物车实现数据存储,并完成点击商品件数跳转到购物车页面
    addEvent(data) {
        this.paging_list.addEventListener('click', (e) => {
            let target = e.target || e.srcElement
            // 获取button按钮
            if (target.nodeName == 'BUTTON') {
                // 点击按钮拿到当前存储在local Storage中的id
                let id = target.dataset.id - 0
                // 当前的商品和data中的商品用id做判断找到相应的数据
                let goods = data.find(item => { return item.goods_id == id })
                // 做数据存储使用
                let list = JSON.parse(localStorage.getItem('cart')) || []
                //判断 list的长度就是在判断有没有数据,没有先存到然后数据就+1,有的话就++,判断点击是否相同不是存起再+1,如此往复
                if (!list.length) {
                    list.push(goods)
                    goods.cart_number = 1
                } else {
                    // 使用some判断当前点击的按钮所属的数据是否与data中的数据的id匹配,如果是则返回true
                    let res = list.some(item => { return item.goods_id == id })
                    if (res) {
                        let index = list.findIndex(item => item.goods_id == id)
                        list[index].cart_number++
                    } else {
                        list.push(goods)
                        goods.cart_number = 1
                    }
                }
                //  把goods加入到list中并且有 goods.cart_number的那条数据存到 localStorage里面
                localStorage.setItem('cart',JSON.stringify(list))
                this.totalNum()
            }

        }, false)
    }
    // 把每天数据的goods.cart_number数量赋值
    totalNum() {
        // 获得list进行遍历,把每一项的cart_number给num,然后把num加在span中
        let list = JSON.parse(localStorage.getItem('cart')) || []
        let num = 0
        list.forEach(item => {
            num += item.cart_number
        })
        this.cartNum.innerHTML = num
    }
}
new more()
   